/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo4;

import Modelo.DAL.ObjetoPerdido_DAO;
import Acceso.Security;
import Modelo.ObjetoPerdido;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author dayana
 */
public class modificarObjetoPerdidoAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Director DSI", "Jefe Apoyo Administrativo","Personal Administrativo"};
       if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }

        System.out.println("Paso Autenticación");

        modificarObjetoPerdidoForm formulario = (modificarObjetoPerdidoForm) form;
        String idObj = formulario.getIdObj();
        String status = formulario.getNew_status();
        String descripcion = formulario.getNew_descrip();
        String tipo = formulario.getTipoObj();

        ActionErrors errors = new ActionErrors();
        boolean resul = ObjetoPerdido_DAO.modificarObjetoPerdido(Long.parseLong(idObj), status, descripcion);

        if (descripcion == null || descripcion.length() < 1 || descripcion == " ") {
            errors.add("campo_descripcion_requerido", new ActionMessage("errors.campo_descripcion_requerido"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }

        if (!resul) {
            errors.add("objeto_no_modificado", new ActionMessage("errors.objeto_no_modificado"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }

        errors.add("objeto_modificado", new ActionMessage("errors.objeto_modificado"));
        saveErrors(request, errors);

        return mapping.findForward(SUCCESS);
    }
}
